package com.hanxu.mallproduct.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hanxu.mallproduct.entity.CategoryEntity;
import com.hanxu.mallproduct.service.CategoryService;
import com.hanxu.mallproduct.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;

    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        List<CategoryEntity> categoryEntities=categoryService.getLevel1Categories();
        model.addAttribute("categories",categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String,List<Catelog2Vo>> getCatelogJson() throws JsonProcessingException {
        Map<String,List<Catelog2Vo>> catelogJson=categoryService.getCatalogJson();
        return catelogJson;
    }
}
